// pages/mine_edit/index.js
const App = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        user: wx.getStorageSync('user')
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        const eventChannel = this.getOpenerEventChannel()
        eventChannel.on('acceptDataFromOpenerPage', (item) => {
            wx.setNavigationBarTitle({ title: item.text });
            this.setData({ btnText: item.text });
            if (item.text == "修改密码") {
                let { user } = this.data;
                this.setData({name:user.name, phone: user.account, filed_disabled: true });
            }
        });
    },

    onChange(e) {
        let { key } = e.currentTarget.dataset;
        let data = {};
        data[key] = e.detail;
        this.setData(data);
    },
    async onEdit(e) {
        let {name, phone, password, btnText } = this.data;
        if (!phone || !password) {
            App.showModal("手机号码和密码不能为空");
            return
        }
        if (!(/^1\d{10}$/.test(phone))) {
            App.showModal("手机号码格式有误");
            return
        }
        if (password.length < 6 || password.length > 20) {
            App.showModal("密码长度为6~20位");
            return;
        }

        await App.$http('/register', { name,phone, password });
        App.showModal(btnText + '成功', () => {
            if (btnText == "修改密码") {
                wx.clearStorageSync();
                wx.reLaunch({
                    url: '/pages/login/index',
                });
            } else {
                wx.navigateBack()
            }
        });
    }
})